// xilinx91-edk-keygen.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <stdlib.h>

//Constants
#define MAX5 100000
#define MAX4 10000
#define MAX3 1000
#define DIVIDER 0x2710

//Different depending on which version to be installed (Evaluation vs Full)
#define ADDER 0x200
#define MULTIPLIER 0x300
#define FIRST_TERM 340


int main(int argc, char * argv[])
{
	int random;
	char third_term[6];
	char second_term[5];
	char first_term[4];
	char fourth_term[5];
	char key[20];

	puts("Xilinx 9.2i EDK Key Generator\nPress enter to generate a new key, press q then enter to exit");
	do {
		random = rand();
		sprintf(third_term,"%05d",random%MAX5);
		sprintf(second_term,"%04d",(((random%MAX5+ADDER)*MULTIPLIER)%DIVIDER)%MAX4);
		sprintf(first_term,"%03d",FIRST_TERM%MAX3);
		sprintf(fourth_term,"%04d",(random>>2)%MAX4); // Can be anything and is still valid (might be different for different products)

		key[0] =					third_term[0];
		key[1] =			second_term[0];
		key[2] =					third_term[1];
		key[3] =	first_term[0];

		key[4] = '-';

		key[5] =			second_term[1];
		key[6] =					third_term[2];
		key[7] =	first_term[1];
		key[8] =			second_term[2];

		key[9] = '-';

		key[10] =					third_term[3];
		key[11] =	first_term[2];
		key[12] =			second_term[3];
		key[13] =					third_term[4];

		key[14] = '-';

		key[15] = fourth_term[2];
		key[16] = fourth_term[3];
		key[17] = fourth_term[0];
		key[18] = fourth_term[1];
		key[19] = 0;

		printf("\r%s",key);

	} while (fgetc(stdin) != 'q');
	return 0;
}

